﻿using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;
using System;
using System.Collections.Generic;
using System.Text;

namespace Utils
{
	public class LoggerUtils
	{
		public static void InitSerilog()
		{
			#region 日志初始化
			string sequrl = CommonTools.GetConfigValue<string>("serilogroot:sequrl");			
			string logfile = CommonTools.GetConfigValue<string>("serilogroot:logfile");
			var logCfg = new LoggerConfiguration().MinimumLevel.Verbose();
			if (!string.IsNullOrEmpty(sequrl))
			{
				int seqlevel = CommonTools.GetConfigValue<int>("serilogroot:seqlevel");
				LogEventLevel level = (LogEventLevel)seqlevel;
				logCfg.WriteTo.Seq(sequrl,level);
			}
			if (!string.IsNullOrEmpty(logfile))
			{
				int logfilelevel = CommonTools.GetConfigValue<int>("serilogroot:logfilelevel");
				LogEventLevel level = (LogEventLevel)logfilelevel;
				logCfg.WriteTo.RollingFile(new CompactJsonFormatter(), logfile, 
					level, 
					fileSizeLimitBytes: 1024 * 1024 * 2, 
					retainedFileCountLimit: 10);

			}
			Log.Logger = logCfg.CreateLogger();
			#endregion
		}
	}
}
